//
//  WDBannerManager.m
//  WDPolymerizeSDK
//
//  Created by little People on 2023/6/1.
//

#import "WDBannerManager.h"
#import "WDAdStatusManager.h"
#import "WDCallBackManager.h"
#import "WDAdStatusManager.h"
#import "WDADManager.h"
#import <AppLovinSDK/AppLovinSDK.h>
#import "WDJsonManager.h"
@import Adjust;

@interface WDBannerManager ()<MAAdViewAdDelegate>
@property (nonatomic, assign) BOOL hasShowAd;
@property (nonatomic, assign)BOOL isReloadBanner;
@property (nonatomic, assign)BOOL isTop;//banner是否顶部
@property (nonatomic, strong) MAAdView *adView;

@end
@implementation WDBannerManager
static WDBannerManager *_banner = nil;
+ (instancetype)shareBannerManager
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _banner = [[self alloc] init];
        
    });
    return _banner;
}

- (void)loadBanner
{

    [[WDAdStatusManager shareStatusManager] adLoadWithEventName:BANNER_EVENT_NAME];
    __block WDBannerManager* bself = self;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
        [bself.adView loadAd];
            
    });
}


- (void)setBannerDelegate
{
 
    if([WDJsonManager maxBannerForSDKConfigFile]&&[WDJsonManager maxBannerForSDKConfigFile].length>4){
        UIViewController *rootViewController = [[[UIApplication sharedApplication].windows objectAtIndex:0] rootViewController];
        self.adView = [[MAAdView alloc] initWithAdUnitIdentifier: [WDJsonManager maxBannerForSDKConfigFile]];
        self.adView.delegate = self;
        // Banner height on iPhone and iPad is 50 and 90, respectively
        CGFloat height = (UIDevice.currentDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad) ? 90 : 50;

        // Stretch to the width of the screen for banners to be fully functional
        CGFloat width = CGRectGetWidth(UIScreen.mainScreen.bounds);

        self.adView.frame = CGRectMake(0 ,rootViewController.view.frame.size.height - height - 20, width, height);

        // Set background or background color for banners to be fully functional
        
        NSLog(@"----%f-----%f",self.adView.frame.size.height,self.adView.frame.size.width);

        [rootViewController.view addSubview: self.adView];
        self.adView.hidden = YES;

        // Load the ad
        [self.adView loadAd];
    }

}

- (void)showBanner
{
    
    if (self.adView){
        [[WDAdStatusManager shareStatusManager] adShowWithEventName:BANNER_EVENT_NAME];

        self.adView.hidden = NO;
        [self loadBanner];

    }
}

- (void)closeBanner
{
    [[WDAdStatusManager shareStatusManager] adCloseWithEventName:BANNER_EVENT_NAME];

    if (self.adView){
        self.adView.hidden = YES;
        
        if ([WDCallBack shareCallBack].onBannerRemoved)
        {
            [WDCallBack shareCallBack].onBannerRemoved(banner);
        }
    }

  
    
    [self loadBanner];
}

- (void)setBannerDirectionWithTop:(BOOL)topDir
{
    self.isTop = topDir;
}



#pragma mark - MAAdDelegate Protocol

- (void)didLoadAd:(MAAd *)ad {
    NSLog(@"--------%s",__PRETTY_FUNCTION__);
}

- (void)didFailToLoadAdForAdUnitIdentifier:(NSString *)adUnitIdentifier withError:(MAError *)error {
    NSLog(@"--------%s",__PRETTY_FUNCTION__);

}

- (void)didClickAd:(MAAd *)ad {
    NSLog(@"--------%s",__PRETTY_FUNCTION__);

}

- (void)didFailToDisplayAd:(MAAd *)ad withError:(MAError *)error {
    NSLog(@"--------%s",__PRETTY_FUNCTION__);

}

#pragma mark - MAAdViewAdDelegate Protocol

- (void)didExpandAd:(MAAd *)ad {
    NSLog(@"--------%s",__PRETTY_FUNCTION__);

}

- (void)didCollapseAd:(MAAd *)ad {
    NSLog(@"--------%s",__PRETTY_FUNCTION__);

}



#pragma mark - Deprecated Callbacks

- (void)didDisplayAd:(MAAd *)ad { /* DO NOT USE - THIS IS RESERVED FOR FULLSCREEN ADS ONLY AND WILL BE REMOVED IN A FUTURE SDK RELEASE */
    
    
    NSNumber *revenue =[NSNumber numberWithDouble:ad.revenue*1000];
    [[WDAdStatusManager shareStatusManager] adRevenue:ad.networkPlacement source:ad.networkName revenue:revenue currency:@"USD"];
    
    NSLog(@"--------%s------%@------%@",__PRETTY_FUNCTION__,ad.description,revenue);

    
    
    ADJAdRevenue *adjustAdRevenue = [[ADJAdRevenue alloc] initWithSource: ADJAdRevenueSourceAppLovinMAX];
    [adjustAdRevenue setRevenue: ad.revenue currency: @"USD"];
    [adjustAdRevenue setAdRevenueNetwork: ad.networkName];
    [adjustAdRevenue setAdRevenueUnit: ad.adUnitIdentifier];
    [adjustAdRevenue setAdRevenuePlacement: ad.placement];
    [Adjust trackAdRevenue: adjustAdRevenue];
    

}
- (void)didHideAd:(MAAd *)ad { /* DO NOT USE - THIS IS RESERVED FOR FULLSCREEN ADS ONLY AND WILL BE REMOVED IN A FUTURE SDK RELEASE */
    NSLog(@"--------%s",__PRETTY_FUNCTION__);

}


@end
